<!--
 Copyright (C) 2005 by Object Mentor, Inc.
 Released under the terms of the GNU General Public License version 2 or later.
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Client Side Sort Test</title>
<script language="JavaScript" type="text/javascript" src="jsUnitTestLink.js"></script>
<script language="JavaScript" type="text/javascript" src="clientSideSort.js"></script>
<script language="JavaScript" type="text/javascript">

var sorter;
var table;

function setUp()
{
  createTable('tableData');
  sorter = new TableSorter('tableData', new DateParser(getRegex(), 8, 2, 3, 4, 5, 6));
}

function tearDown()
{
  document.body.removeChild(document.getElementById('tableData'));
}

function getRegex()
{
  return /^(\w+) (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) (\d+) (\d+).(\d+).(\d+) (\w+) (\d+)$/;
}

function testCorrectTable()
{
  assertEquals(table, sorter.table);
}

function testGetSortDirection()
{
  assertEquals("asc", sorter.getSortDirection(0));
  assertEquals("dsc", sorter.getSortDirection(0));
  assertEquals("asc", sorter.getSortDirection(0));
  assertEquals("asc", sorter.getSortDirection(1));
}

function testParseMonth()
{
  parser = new DateParser();
  assertEquals(0, parser.parseMonth('Jan'));
  assertEquals(0, parser.parseMonth('jan'));
  assertEquals(4, parser.parseMonth('May'));
  assertEquals(4, parser.parseMonth('may'));
  assertEquals(11, parser.parseMonth('Dec'));
  assertEquals(11, parser.parseMonth('dec'));
}

function testNumericComparator()
{
  a = 2;
  b = 12;
  comparator = new NumericComparator();
  assertEquals(0, comparator(a, a));
  assertEquals(0, comparator(b, b));
  assertEquals(-1, comparator(a, b));
  assertEquals(1, comparator(b, a));
}

function testSortReference()
{
  a = new Object();
  b = new Object();
  ref = new SortReference(a, b);
  assertEquals(a, ref.column);
  assertEquals(b, ref.direction);
}

function testDateComparator()
{
  a = 'Thu Mar 31 14:44:55 CST 2005';
  b = 'Fri Apr 15 22:55:47 CST 2005';
  comparator = new DateComparator(new DateParser(getRegex(), 8, 2, 3, 4, 5, 6));
  assertEquals(0, comparator(a, a));
  assertEquals(0, comparator(b, b));
  assertEquals(-1, comparator(a, b));
  assertEquals(1, comparator(b, a));
}

function testDateParser()
{
  parser = new DateParser(getRegex(), 8, 2, 3, 4, 5, 6);
  date = new Date(2005, 3, 5, 22, 55, 47);
  assertEquals(date.getTime(), parser.parse('Tue Apr 05 22:55:47 CDT 2005').getTime());
}

function testDateParserOtherRegex()
{
  regex = /^(\d+)\/(\d+)\/(\d+)$/;
  parser = new DateParser(regex, 3, 1, 2);
  date = new Date(1963, 10, 26);
  assertEquals(date.getTime(), parser.parse('11/26/1963').getTime());
}

function testDataRowDefault()
{
  tableRowData = new TableRowData("a,b,c".split(','));
  assertEquals("a", tableRowData.toString());
}

function testDataRowSetSortCol()
{
  tableRowData = new TableRowData("a,b,c".split(','));
  tableRowData.setSortCol(1);
  assertEquals("b", tableRowData.toString());
}

function testDataRowArray()
{
  setText('1,1', 0, 0);
  setText('1,2', 0, 1);
  setText('2,1', 1, 0);
  setText('2,2', 1, 1);
  setText('3,1', 2, 0);
  setText('3,2', 2, 1);
  tableRowDataArray = new TableRowDataArray('tableData');
  assertEquals(3, tableRowDataArray.length);
  assertEquals(2, tableRowDataArray[0].cols.length);
  assertEquals(2, tableRowDataArray[1].cols.length);
  assertEquals(2, tableRowDataArray[2].cols.length);
  assertEquals('1,1', tableRowDataArray[0].cols[0]);
  assertEquals('1,2', tableRowDataArray[0].cols[1]);
  assertEquals('2,1', tableRowDataArray[1].cols[0]);
  assertEquals('2,2', tableRowDataArray[1].cols[1]);
  assertEquals('3,1', tableRowDataArray[2].cols[0]);
  assertEquals('3,2', tableRowDataArray[2].cols[1]);
  assertEquals('row1', getCssClass(0));
  assertEquals('row2', getCssClass(1));
  assertEquals('row3', getCssClass(2));
}

function testRebuildTableAfterSort()
{
  setText('b', 0, 0);
  setText('b2', 0, 1);
  setText('c', 1, 0);
  setText('c2', 1, 1);
  setText('a', 2, 0);
  setText('a2', 2, 1);
  tableRowDataArray = new TableRowDataArray('tableData');
  tableRowDataArray.sort();
  tableRowDataArray.rebuildTable();
  assertEquals('a', getText(0, 0));
  assertEquals('a2', getText(0, 1));
  assertEquals('b', getText(1, 0));
  assertEquals('b2', getText(1, 1));
  assertEquals('c', getText(2, 0));
  assertEquals('c2', getText(2, 1));
}

function testDateSort()
{
  date1 = 'Thu Mar 31 14:44:55 CST 2005';
  date2 = 'Wed Apr 06 22:36:46 CDT 2005';
  date3 = 'Thu Apr 07 21:35:35 CDT 2005';

  setText(date2, 0, 0);
  setText(date3, 1, 0);
  setText(date1, 2, 0);
  sorter.sort(0, 'date');
  assertEquals(date1, getText(0, 0));
  assertEquals(date2, getText(1, 0));
  assertEquals(date3, getText(2, 0));

  assertEquals('row1', getCssClass(0));
  assertEquals('row2', getCssClass(1));
  assertEquals('row3', getCssClass(2));
}

function setText(text, row, col)
{
  table = document.getElementById('tableData');
  table.tBodies[0].rows[row].cells[col].innerHTML = text;
}

function getText(row, col)
{
  table = document.getElementById('tableData');
  return table.tBodies[0].rows[row].cells[col].innerHTML;
}

function getCssClass(row)
{
  table = document.getElementById('tableData');
  return table.tBodies[0].rows[row].className;
}

function createTable(id)
{
  table = document.createElement('table');
  table.setAttribute('id', id);
  tbody = document.createElement('tbody');

  tr1 = document.createElement('tr');
  tr1.className = 'row1';
  tr2 = document.createElement('tr');
  tr2.className = 'row2';
  tr3 = document.createElement('tr');
  tr3.className = 'row3';

  td1 = document.createElement('td');
  td2 = document.createElement('td');
  td3 = document.createElement('td');
  td4 = document.createElement('td');
  td5 = document.createElement('td');
  td6 = document.createElement('td');

  table.appendChild(tbody);
  tbody.appendChild(tr1);
  tbody.appendChild(tr2);
  tbody.appendChild(tr3);
  tr1.appendChild(td1);
  tr1.appendChild(td2);
  tr2.appendChild(td3);
  tr2.appendChild(td4);
  tr3.appendChild(td5);
  tr3.appendChild(td6);

  document.body.appendChild(table);
}

</script>
</head>
<body>
</body>
</html>
